package com.cx.controller.back;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cx.entity.Comment;
import com.cx.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.cx.service.CommentService;
import com.cx.utils.R;

import javax.servlet.http.HttpServletRequest;

/**
 *
 *
 * @author cx
 * @email cx@163.com
 * @date 2024-05-09 10:29:02
 */
//@RestController

@Controller
@RequestMapping("/back/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;
    @Autowired
    private ArticleService articleService;

//    到达评论列表页
    @GetMapping("/list.html")
    public String toList() {
        return "back/page/comment/list";
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("cx:comment:list")
    public @ResponseBody R list(@RequestParam Map<String, Object> params){
        IPage<Comment> page = commentService.getCommentListAndArticlePageConditions(params);
        return R.ok().put("count",page.getTotal()).put("data",page.getRecords());
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("cx:comment:info")
    public R info(@PathVariable("id") Integer id){
		Comment comment = commentService.getById(id);

        return R.ok().put("comment", comment);
    }

    /*
    * 进入到添加页面的接口
    * */
    @GetMapping("/add.html")
    public String toAdd(){
        return "back/page/comment/add";
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("cx:comment:save")
    public@ResponseBody R save(Comment comment, HttpServletRequest request){
        String remoteAddr = "";
        if (request != null) {
            remoteAddr = request.getHeader("X-FORWARDED_FOR");
            if (remoteAddr == null || "".equals(remoteAddr)) {
                remoteAddr = request.getRemoteAddr();
            }
        }
        comment.setIp(remoteAddr);
        comment.setCreated(new Date());
        //目前默认admin，待加了安全认证登录后获取已登录用户的用户名
        comment.setAuthor("admin");
		commentService.save(comment);

        return R.ok();
    }

    /*
     * 进入编辑页面：携带数据（当前修改的评论数据、文章列表）
     * */
    @GetMapping("/edit.html")
    public String toEdit(@RequestParam Integer id, Model model) {
        model.addAttribute("comment", commentService.getById(id));
        model.addAttribute("articleList", articleService.getAll());
        return "back/page/comment/edit";
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("cx:comment:update")
    public @ResponseBody R update(Comment comment){
        System.out.println("========"+comment);
		commentService.updateById(comment);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("cx:comment:delete")
    public @ResponseBody R delete(@RequestBody Integer[] ids){//[1,2]
		commentService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
